package org.iteam.core.utils;

import java.util.Map;

/**
 * 分类控件，最重要的分页类
 * 
 * @author 文科
 * 
 */
public class Pagination {
	/** 返回总页数 */
	private long totalPage;

	/** 返回前台页面分页页码最前面的页码 */
	private long startpage;

	/** 返回前台页面分页页码最后面的页码 */
	private long endPage;

	/** 页码封装 */
	private String paging;

	/** 默认每页显示记录数 */
	public static long DEF_PAGE_SIZE = 10;

	/** 前台页面显示页码数量，[1][2][3]... */
	public static long DEF_PAGE_COUNT = 9;

	/** 获得开始记录索引 ，通过当前页号计算 */
	public long getStartIndex() {
		long startIndex = (pageNo - 1) * pageSize;
		return startIndex;
	}

	/** 获得总页数 */
	public long getTotalPage() {
		long result = (int) (getTotalCount() / pageSize);
		totalPage = (getTotalCount() % pageSize == 0) ? result : result + 1;
		return totalPage;
	}

	/** 获得上页的页号 */
	public long getPrePage() {
		return pageNo - 1 < 1 ? 1 : pageNo - 1;
	}

	/** 获得下页的页号 */
	public long getNextPage() {
		return pageNo + 1 > getTotalPage() ? getTotalPage() : pageNo + 1;
	}

	/**
	 * 组装分页样式与跳转超链接（默认样式）
	 * 
	 * @param page
	 *            返回给前台的page对象
	 * @returnr
	 */
	public String defalutSytlePaging(Map<String, String> urlParams, String url) {
		/** 组装页码 */
		StringBuilder sb = new StringBuilder();
		if(getTotalCount()>0){
			// 如果action后缀未作处理，则去掉replace
			String action = url;
			// 设置查询参数
			String queryStr = "?page=";
			String queryParams = "";
			if (urlParams != null && urlParams.size() > 0) {
				for (String k : urlParams.keySet()) {
					if(urlParams.get(k)!=null&&!urlParams.get(k).equals("")){
						queryParams += "&" + k + "=" + urlParams.get(k);
					}
				}
			}
			
			// 如果只有一页，则直接返回空字符串
			if (getTotalPage() == 0) {
				return sb.toString();
			}
			if(pageNo==1){
				// 上一页
				sb.append("<a href='javascript:void(0)'>上一页</a>");
			}else{
				// 上一页
				sb.append("<a href=\"" + action + queryStr + getPrePage() + queryParams
						+ "\">上一页</a>");
			}
			
			// 超过三页则显示第一页链接
			if (getStartpage() >= 3) {
				sb.append("<a href=\"" + action + queryStr + "1" + queryParams
						+ "\">1</a>");
				sb.append("<a href=\"" + action + queryStr
						+ (getPageNo() - (int) Math.ceil(page / 2.0)) + queryParams
						+ "\">…</a>");
			}
			// 中间页
			for (long i = getStartpage(); i <= getEndPage(); i++) {
				if (pageNo == i) {
					sb.append("<span class=\"current\">" + i + "</span>");
				} else {
					sb.append("<a href=\"" + action + queryStr + i + queryParams
							+ "\">" + i + "</a>");
				}
			}
			// 最后页小于总页数，则显示最后一页
			if (getEndPage() < getTotalPage()) {
				sb.append("<a href=\"" + action + queryStr
						+ (getPageNo() + (int) Math.ceil(page / 2.0)) + queryParams
						+ "\">…</a>");
				sb.append("<a href=\"" + action + queryStr + getTotalPage()
						+ queryParams + "\">" + getTotalPage() + "</a>");
			}
			if(pageNo==getTotalPage()){
				// 下一页
				sb.append("<a href='javascript:void(0)'>下一页</a>");
			}else{
				// 下一页
				sb.append("<a href=\"" + action + queryStr + getNextPage()
						+ queryParams + "\" class=\"next\">下一页</a>");
			}
		}else{
			sb.append("<span><font color='red' style='font-size:12px;'>暂无任何相关数据</font></span>");
		}
		
		
		return sb.toString();
	}

	private long page;

	private void getPageIndex(long page) {
		page = (page < 0) ? DEF_PAGE_COUNT : page;
		this.setPage(page);
		long startPage = pageNo - (page % 2 == 0 ? page / 2 - 1 : page / 2);
		long endPage = pageNo + (page % 2 == 0 ? page / 2 - 1 : page / 2);
		if (startPage < 1) {
			startPage = 1;
			if (getTotalPage() >= page) {
				endPage = page;
			} else {
				endPage = getTotalPage();
			}
		}
		if (endPage > getTotalPage()) {
			endPage = getTotalPage();
			if ((endPage - page) > 0)
				startPage = (int) endPage - page + 1;
			else
				startPage = 1;
		}
		this.startpage = startPage;
		this.endPage = (int) endPage;
	}

	/** 查询记录总数 */
	private Long totalCount;

	/** 当前浏览页码 */
	private long pageNo;

	/** 每页显示记录数 */
	private long pageSize;

	/**
	 * 构造与处理分页参数
	 * 
	 * @param pageNo
	 *            当前浏览页码
	 * @param pageSize
	 *            每页显示记录数，设-1为系统默认值 DEF_PAGE_SIZE
	 */
	public Pagination(long pageNo, long pageSize, long totalCount) {
		setPageNo(pageNo);
		if (pageSize <= 0) {
			this.setPageSize(DEF_PAGE_SIZE);
		} else {
			this.setPageSize(pageSize);
		}
		setTotalCount(totalCount);
		getPageIndex(-1);
	}

	public Pagination() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Long getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(Long totalCount) {
		this.totalCount = (totalCount < 0) ? 0 : totalCount;
	}

	public long getPageNo() {
		return pageNo;
	}

	public void setPageNo(long pageNo) {
		this.pageNo = (pageNo < 1) ? 1 : pageNo;
	}

	public long getPageSize() {
		return pageSize;
	}

	public void setPageSize(long pageSize) {
		this.pageSize = (pageSize < 0) ? DEF_PAGE_SIZE : pageSize;
	}

	public long getStartpage() {
		return startpage;
	}

	public void setStartpage(long startpage) {
		this.startpage = startpage;
	}

	public long getEndPage() {
		return endPage;
	}

	public void setEndPage(long endPage) {
		this.endPage = endPage;
	}

	public String getPaging() {
		return paging;
	}

	public void setPaging(String paging) {
		this.paging = paging;
	}

	public void setTotalPage(long totalPage) {
		this.totalPage = totalPage;
	}

	public long getPage() {
		return page;
	}

	public void setPage(long page) {
		this.page = page;
	}

}
